﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Our.Umbraco.uDbCompare
{
    public partial class detail : System.Web.UI.Page
    {
        private string currentConnectionString
        {
            get { return System.Configuration.ConfigurationSettings.AppSettings["umbracoDbDSN"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
               BindData();
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("index.aspx?id={0}", uDbCompare.nodeId));
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            DbCompareItem compareItem = (DbCompareItem)Enum.Parse(typeof(DbCompareItem), Request.QueryString["CompareItem"]);
            string key = Request.QueryString["name"];

            CompareTypeLabel.Text = compareItem.ToString();
            NameLabel.Text = key;

            DataTable dt = uDbCompare.CompareDetails(compareItem, key, currentConnectionString, uDbCompare.RemoteConnectionString);
            
            if (DetailGridView.Columns.Count == 0)
            {
                BoundField name = new BoundField();
                name.DataField = uDbCompare.fieldName;
                name.HeaderText = uDbCompare.fieldName;
                DetailGridView.Columns.Add(name);

                BoundField left = new BoundField();
                left.DataField = uDbCompare.left;
                left.HeaderText = uDbCompare.GetServerName(currentConnectionString, uDbCompare.left);
                DetailGridView.Columns.Add(left);


                BoundField right = new BoundField();
                right.DataField = uDbCompare.right;
                right.HeaderText = uDbCompare.GetServerName(uDbCompare.RemoteConnectionString, uDbCompare.right);
                DetailGridView.Columns.Add(right);
            }

            DetailGridView.RowDataBound += new GridViewRowEventHandler(DetailGridView_RowDataBound);
            DetailGridView.DataSource = dt;
            DetailGridView.DataBind();
        }

        protected void DetailGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv != null)
                {
                    bool leftDataMissing = (bool)drv[uDbCompare.leftDataMissing];
                    bool rightDataMissing = (bool)drv[uDbCompare.rightDataMissing];
                    string field = (string)drv[uDbCompare.fieldName];
                    string leftMatch = (string)drv[uDbCompare.left];
                    string rightMatch = (string)drv[uDbCompare.right];

                    if (leftDataMissing)
                    {
                        SetCell(e.Row.Cells[1], DbCompareMatch.Missing);
                    }
                    else if (rightDataMissing)
                    {
                        SetCell(e.Row.Cells[2], DbCompareMatch.Missing);
                    }
                    else
                    {
                        if (uDbCompare.Compare(field, leftMatch, rightMatch))
                            SetCell(e.Row.Cells[0], DbCompareMatch.Matching);
                        else
                            SetCell(e.Row.Cells[0], DbCompareMatch.NonMatching);
                    }
                }
            }
        }

        private void SetCell(TableCell cell, DbCompareMatch match)
        {
            switch (match)
            {
                case DbCompareMatch.Matching:
                    cell.BackColor = System.Drawing.Color.Green;
                    break;
                case DbCompareMatch.Missing:
                    cell.BackColor = System.Drawing.Color.Red;
                    break;
                case DbCompareMatch.NonMatching:
                    cell.BackColor = System.Drawing.Color.Yellow;
                    break;
            }
        }

    }
}
